<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 300 条记录</span>
          <el-button icon="el-icon-plus" size="small" type="primary" round @click="preview('add')">
            添加面经
          </el-button>
        </div>
      </template>
      <el-table :data="rows">
        <el-table-column prop="stem" label="标题" width="400"> </el-table-column>
        <el-table-column prop="likeCount" label="点赞"> </el-table-column>
        <el-table-column prop="views" label="浏览数"> </el-table-column>
        <el-table-column prop="createdAt" label="更新时间"> </el-table-column>
        <el-table-column label="编辑">
          <template #default="{ row }">
            <div class="actions">
              <i class="el-icon-view" @click="preview('view', row.id)"></i>
              <i class="el-icon-edit-outline" @click="preview('edit', row.id)"></i>
              <i class="el-icon-delete" @click="del(row.id)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background layout="prev, pager, next" :total="total">
      </el-pagination>
    </el-card>

    <el-drawer :title="drawerTitle" size="50%" :visible.sync="drawer" direction="rtl" :before-close="handleClose">
      <el-form ref="myform" label-width="80px" :model="form" :rules="rules">
        <el-form-item label="标题" prop="stem">
          <el-input placeholder="请输入标题" v-model="form.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor v-model="form.content" @blur="$refs.myform.validateField('content')">
          </quill-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="add">确认</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import axios from 'axios'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// import { Message } from 'element-ui'
import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'article-page',
  components: {
    quillEditor
  },
  data () {
    return {
      rows: [], // 列表
      total: 0,
      drawer: false, // 控制抽屉显示和隐藏
      title: '大标题',
      form: {
        stem: '', // 标题
        content: '' // 内容
      },
      myQuillEditor: '',
      // 校验规则
      rules: {
        stem: [{ required: true, message: '请输入面经标题', trigger: ['blur', 'change'] }],
        content: [{ required: true, message: '请输入面经标题', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    async getList () {
      const { data } = await axios.get('http://localhost:3000/query')
      this.rows = data
      this.total = data.length
    },
    handleClose () {
      this.drawer = false
      // 清除表单
      this.$refs.myform.resetFields()
    },
    async preview (type, id) {
      this.title = type === 'add' ? '新增标题' : (type === 'view' ? '预览标题' : '编辑标题')
      this.drawer = true
      console.log(type, id)
      if (type === 'edit') {
        // 回显数据
        const { data } = await axios.get(`http://localhost:3000/query/${id}`)
        this.form = { ...data }
      }
    },
    async del (id) {
      try {
        const res = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        if (res === 'confirm') {
          await axios.delete(`http://localhost:3000/query/${id}`)
          this.$message.success('删除成功')
          this.getList()
        }
      } catch (error) {
        console.log(error)
      }
    },
    async add () {
      try {
        await this.$refs.myform.validate()
        if (this.drawerTitle === '新增标题') {
          // 校验成功后 新增数据
          await axios.post('http://localhost:3000/query', {
            stem: this.form.stem,
            content: this.form.content,
            createdAt: new Date().toLocaleDateString(),
            likeCount: Math.floor(Math.random() * 101),
            views: Math.floor(Math.random() * 1001)
          })
          // Message.success('新增成功')
          this.$message.success('新增成功')
          this.handleClose()
          // 重新加载数据
          this.getList()
        }
        if (this.drawerTitle === '编辑标题') {
          await axios.put(`http://localhost:3000/query/${this.form.id}`, {
            ...this.form
          })
          this.$message.success('编辑成功')
          this.getList()
          this.handleClose()
        }
      } catch (error) {
        console.log(error)
      }
    }
  },
  computed: {
    drawerTitle () {
      return this.title
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }

  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;

    >i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}

.el-pagination {
  margin-top: 20px;
  text-align: center;
}

.el-breadcrumb {
  margin-top: 10px;
}

.el-form {
  padding-right: 40px;
}

.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}

.el-rate {
  padding: 10px 0;
}

.article-preview {
  padding: 0 40px 40px 40px;

  >h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
